<template>
  <h1 style="text-align: center">失物申请</h1>
  <el-form v-loading="loading" :model="lost" ref="lostRef" label-width="120px" :rules ="lostRule" label-position="top">
    <el-form-item label="物品名称">
      <el-input  disabled  v-model="lost.lostName" />
    </el-form-item>
    <el-form-item label="描述">
      <el-input disabled  v-model="lost.fileDesc" type="textarea" />
    </el-form-item>
    <el-form-item label="附件">
      <el-image
        style="width: 100px; height: 100px"
        :src="singleUrl(lost.fileRandomId)"
        :preview-src-list="arrayUrl(lost.fileRandomId)"
        fit="fill"
        lazy
      ></el-image>
    </el-form-item>
    <el-form-item label="失主" prop="lostOwnerName">
      <el-input v-model="lost.lostOwnerName" />
    </el-form-item>
    <el-form-item label="联系方式" prop="tel">
      <el-input type="number" v-model="lost.tel" />
    </el-form-item>
    <el-form-item label="领取人" prop="drawer">
      <el-input v-model="lost.drawer" />
    </el-form-item>
    <el-form-item label="领取人证件号" prop="drawCard">
      <el-input v-model="lost.drawCard" />
    </el-form-item>
    <el-form-item label="领取人电话号码" prop="drawTel">
      <el-input type="number" v-model="lost.drawTel" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="userApply">{{$t('msg.common.apply')}}</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { getById, apply } from '@/api/lost'
import { singleUrl, arrayUrl } from '@/utils/file'
import { validateTel, validateIdentityCard } from '@/utils/validate'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
import router from '@/router'

const loading = ref(false)
const route = useRoute()
const lostId = route.params.id
const lost = ref({
  lostName: '',
  fileDesc: '',
  lostOwnerName: '',
  tel: ''
})
const getLostDetail = async () => {
  getById(lostId).then(res => {
    lost.value = res
  }).catch(err => {
    ElMessage.error(err.message)
  })
}

getLostDetail()
const i18n = useI18n()

const lostRef = ref(null)

const lostRule = ref({
  lostOwnerName: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.lostOwnerName')
    }
  ],
  tel: [
    {
      required: true,
      trigger: 'blur',
      validator: validateTel()
    }
  ],
  drawer: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.drawer')
    }
  ],
  drawCard: [
    {
      required: true,
      trigger: 'blur',
      message: validateIdentityCard()
    }
  ],
  drawTel: [
    {
      required: true,
      trigger: 'blur',
      message: validateTel()
    }
  ]
})

const userApply = async () => {
  lostRef.value.validate(valid => {
    if (!valid) return
    loading.value = true
    lost.value.statusDesc = 'APPLY'
    apply(lost.value).then(res => {
      loading.value = false
      ElMessage.success(i18n.t('msg.common.success') || res.message)
      router.push('/lost/show')
    }).catch(err => {
      ElMessage.error(i18n.t('msg.common.error') || err.message)
      loading.value = false
    })
  })
}

</script>

<style scope>
#app {
    height: 100%;
    width: 90%;
    position: absolute;
    left: 5%;
}
</style>
